package com.example.demo.test.lecture.httpstest;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import com.alibaba.csb.sdk.CsbSDKConstants;
import com.alibaba.csb.sdk.SdkLogger;
import com.alibaba.csb.sdk.security.DefaultSignServiceImpl;
import com.alibaba.csb.sdk.security.ParamNode;
import com.alibaba.csb.sdk.security.SortedParamList;
import com.alibaba.csb.sdk.security.SpasSigner;
import com.alibaba.csb.security.spi.SignService;
import com.alibaba.csb.security.spi.SignServiceRuntime;

import java.util.*;
import java.util.Map.Entry;


public class SignUtil {
    private static final Random random = new Random(System.currentTimeMillis());

    public SignUtil() {
    }

    public static Map<String, String> newParamsMap(Map<String, List<String>> paramsMap, String apiName, String version, String accessKey, String securityKey, boolean timestampFlag, boolean nonceFlag, Map<String, String> extSignHeaders, StringBuffer signDiagnosticInfo, String signImpl, String vefifySignImpl) {
        Map<String, List<String>> newParamsMap = new HashMap();
        Map<String, String> headerParamsMap = new HashMap();
        if (paramsMap != null) {
            newParamsMap.putAll(paramsMap);
        }

        if (apiName != null) {
            newParamsMap.put("_api_name", Arrays.asList(apiName));
            headerParamsMap.put("_api_name", apiName);
        }

        if (version != null) {
            newParamsMap.put("_api_version", Arrays.asList(version));
            headerParamsMap.put("_api_version", version);
        }

        String timestampStr = System.getProperty("timestamp");
        if (timestampStr == null) {
            Long ts = System.currentTimeMillis();
            timestampStr = ts.toString();
        }

        if (nonceFlag && CsbSDKConstants.isNonceEnabled) {
            String nonceStr = System.getProperty("nonce");
            if (nonceStr == null) {
                Long nonce = random.nextLong();
                nonceStr = nonce.toString();
            }

            newParamsMap.put("_api_nonce", Arrays.asList(nonceStr));
            headerParamsMap.put("_api_nonce", nonceStr);
        }

        if (timestampFlag) {
            newParamsMap.put("_api_timestamp", Arrays.asList(timestampStr));
            headerParamsMap.put("_api_timestamp", timestampStr);
        }

        if (extSignHeaders != null) {
            Iterator var24 = extSignHeaders.entrySet().iterator();

            while(var24.hasNext()) {
                Entry<String, String> kv = (Entry)var24.next();
                newParamsMap.put(kv.getKey(), Arrays.asList((String)kv.getValue()));
                headerParamsMap.put(kv.getKey(), kv.getValue());
            }
        }

        vefifySignImpl = SignServiceRuntime.pickVerifySignImplName(vefifySignImpl);
        if (vefifySignImpl != null) {
            newParamsMap.put("_api_verify_sign_impl", Arrays.asList(vefifySignImpl));
            headerParamsMap.put("_api_verify_sign_impl", vefifySignImpl);
        }

        if (accessKey != null) {
            headerParamsMap.put("_api_access_key", accessKey);
            newParamsMap.put("_api_access_key", Arrays.asList(accessKey));
            newParamsMap.remove("_api_signature");
            newParamsMap.remove("_api_secret_key");
            long currT = System.currentTimeMillis();
            SignService signService = SignServiceRuntime.pickSignService(signImpl);
            signImpl = signService.getClass().getCanonicalName();
            System.out.println("signImpl: "+signImpl);
            if (!signImpl.equals(DefaultSignServiceImpl.class.getCanonicalName())) {
                newParamsMap.put("_api_sign_impl", Arrays.asList(signImpl));
                headerParamsMap.put("_api_sign_impl", signImpl);
                System.out.println("+++++++++++++++++");
            }
            System.out.println("------------------------------------");
            for(Map.Entry<String, List<String>> entry : newParamsMap.entrySet()){
                System.out.println(entry.getKey());
            }
            SortedParamList paramNodeList = convertMultiValueParams(newParamsMap);
            String signKey = signService.generateSignature(paramNodeList, accessKey, securityKey);
            if (SdkLogger.isLoggable() || signDiagnosticInfo != null) {
                StringBuffer msg = new StringBuffer();
                msg.append("sign parameters:\n");
                boolean first = true;

                for(Iterator var21 = newParamsMap.keySet().iterator(); var21.hasNext(); first = false) {
                    String key = (String)var21.next();
                    if (!first) {
                        msg.append(", ");
                    }

                    msg.append(String.format("%s=%s", key, newParamsMap.get(key)));
                }

                msg.append("===signature:" + signKey).append(", ").append("===costs time:" + (System.currentTimeMillis() - currT) + "ms");
                if (signDiagnosticInfo != null) {
                    signDiagnosticInfo.setLength(0);
                    signDiagnosticInfo.append(msg.toString());
                }

                if (SdkLogger.isLoggable()) {
                    SdkLogger.print(msg.toString());
                }
            }

            headerParamsMap.put("_api_signature", signKey);
        }

        return headerParamsMap;
    }

    public static SortedParamList convertMultiValueParams(Map<String, List<String>> map) {
        SortedParamList pnList = new SortedParamList();
        if (map == null) {
            return pnList;
        } else {
            Iterator var3 = map.entrySet().iterator();

            while(true) {
                while(var3.hasNext()) {
                    Entry<String, List<String>> entry = (Entry)var3.next();
                    String key = (String)entry.getKey();
                    List<String> vlist = (List)entry.getValue();
                    if (vlist == null) {
                        ParamNode node = new ParamNode(key, (String)null);
                        pnList.add(node);
                    } else {
                        Iterator var6 = vlist.iterator();

                        while(var6.hasNext()) {
                            String v = (String)var6.next();
                            ParamNode node = new ParamNode(key, v);
                            pnList.add(node);
                        }
                    }
                }

                return pnList;
            }
        }
    }

    private static SortedParamList convertSingleValueParms(Map<String, String> map) {
        SortedParamList sortedParamList = new SortedParamList();
        if (map != null) {
            Iterator var2 = map.entrySet().iterator();

            while(var2.hasNext()) {
                Entry<String, String> entry = (Entry)var2.next();
                String key = (String)entry.getKey();
                ParamNode node = new ParamNode(key, (String)entry.getValue());
                sortedParamList.add(node);
            }
        }

        return sortedParamList;
    }

    public static String sign(Map<String, String> paramsMap, String secretKey) {
        return sign(convertSingleValueParms(paramsMap), secretKey);
    }

    private static String sign(SortedParamList sortedParamList, String secretKey) {
        return SpasSigner.sign(sortedParamList, secretKey);
    }

    public static void warmup() {
        SignServiceRuntime.pickSignService((String)null).generateSignature(new SortedParamList(), "ak", "sk");
    }
}

